package net.wofly.common.pay.service;

import com.alipay.api.AlipayApiException;
import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.wx.bean.WxTransactionType;
import net.wofly.cashier.order.util.PayType;
import net.wofly.common.pay.PayContext;
import net.wofly.common.pay.PayProcessor;
import net.wofly.common.pay.alipay.service.AliPayService;
import net.wofly.common.pay.util.ClientType;
import net.wofly.framework.util.ResultCode;
import net.wofly.framework.util.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;

@Service
public class PayService {
    
    @Autowired
    AliPayService aliPayService;
    
    public ReturnBean<String> doPayOld(
            String orderID, Integer payType, Integer clientType,
            String totalAmount) throws IOException, AlipayApiException {
        String result = null;
        if (PayType.支付宝.getValue().intValue() == payType) {
            result = aliPayService.pay(clientType, orderID, totalAmount, "订单" + orderID + "支付", "");
        } else if (PayType.微信支付.getValue().intValue() == payType) {
            return new ReturnBean<>(ResultCode.ERROR_SERVICE, "暂不支持微信支付，系统开发中");
        } else if (PayType.现金.getValue().intValue() == payType) {
            // TODO 现金 支付逻辑
            return new ReturnBean<>(ResultCode.SUCCESS, "现金支付请走线下通道", result);
        } else {
            return new ReturnBean<>(ResultCode.ERROR_SERVICE, "支付方式不正确");
        }
        return new ReturnBean<>(ResultCode.SUCCESS, "调用支付接口成功", result);
    }
    
    public ReturnBean<String> doPay(
            String orderID, String orderNo, String orderDesc, Integer payType, Integer clientType,
            String totalAmount) throws IOException, AlipayApiException {
        String result = null;
        if (PayType.支付宝.getValue().intValue() == payType || PayType.微信支付.getValue().intValue() == payType) {
            PayContext payContext = new PayContext();
            payContext.setClientType(clientType);
            PayOrder payOrder = new PayOrder("订单" + orderNo, orderDesc,
                    null == totalAmount ? new BigDecimal(0.01) : new BigDecimal(totalAmount), orderID);
            if (PayType.支付宝.getValue().intValue() == payType) {
                if (ClientType.PC端.getValue().intValue() == clientType) {
                    payOrder.setTransactionType(AliTransactionType.DIRECT);
                } else if (ClientType.移动端_H5.getValue().intValue() == clientType) {
                    payOrder.setTransactionType(AliTransactionType.WAP);
                } else if (ClientType.移动端_APP.getValue().intValue() == clientType) {
                    payOrder.setTransactionType(AliTransactionType.APP);
                }
                payContext.setPayOrder(payOrder);
                result = PayProcessor.payChannelMap.get(PayType.支付宝.getValue()).process(payContext).getContent();
            } else if (PayType.微信支付.getValue().intValue() == payType) {
                if (ClientType.PC端.getValue().intValue() == clientType) {
                    payOrder.setTransactionType(WxTransactionType.NATIVE);
                } else if (ClientType.移动端_H5.getValue().intValue() == clientType) {
                    payOrder.setTransactionType(WxTransactionType.MWEB);
                } else if (ClientType.移动端_APP.getValue().intValue() == clientType) {
                    payOrder.setTransactionType(WxTransactionType.APP);
                }
                payContext.setPayOrder(payOrder);
                result = PayProcessor.payChannelMap.get(PayType.微信支付.getValue()).process(payContext).getContent();
            }
            
        } else if (PayType.现金.getValue().intValue() == payType) {
            // TODO 现金 支付逻辑
            return new ReturnBean<>(ResultCode.SUCCESS, "现金支付请走线下通道", result);
        } else {
            return new ReturnBean<>(ResultCode.ERROR_SERVICE, "支付方式不正确");
        }
        return new ReturnBean<>(ResultCode.SUCCESS, "调用支付接口成功", result);
    }
}
